﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameSystem : MonoBehaviour
{
    private Dictionary<string, GameObject> _dictionary;

    void Awake()
    {
        _dictionary = new Dictionary<string, GameObject>();
    }

    void Start()
    {
        Engine.GameSystem = this;

        Engine.GameSystem.Load("System/TipSystem");
        Engine.GameSystem.Load("System/UISystem");
        Engine.GameSystem.Load("System/EventSystem");
    }
	
	void Update()
    {
	    
	}

    public GameObject Load(string name)
    {
        Object origin = Resources.Load("Prefab/" + name);
        return Load(name, origin);
    }

    public GameObject Load(string name, Object origin, Vector3 position = new Vector3())
    {
        GameObject gameObject = Instantiate(origin, position, new Quaternion(), transform) as GameObject;
        _dictionary.Add(name, gameObject);
        return gameObject;
    }

    public void UnLoad(string name)
    {
        Destroy(_dictionary[name]);
        _dictionary.Remove(name);
    }

    public UserModel ReadUser()
    {
        UserModel user = new UserModel();
        user.userName = PlayerPrefs.GetString("userName", "");
        user.level = PlayerPrefs.GetInt("level", 0);
        user.exp = PlayerPrefs.GetInt("exp", 0);
        user.gold = PlayerPrefs.GetInt("gold", 0);
        user.crystal = PlayerPrefs.GetInt("crystal", 0);
        return user;
    }

    public void WriteUser(UserModel user)
    {
        PlayerPrefs.SetString("userName", user.userName);
        PlayerPrefs.SetInt("level", user.level);
        PlayerPrefs.SetInt("exp", user.exp);
        PlayerPrefs.SetInt("gold", user.gold);
        PlayerPrefs.SetInt("crystal", user.crystal);

        //LocalStoreHelper.Store(user, Engine.File("UserData"));
    }

    public void Reset()
    {
        PlayerPrefs.DeleteAll();
    }

    public StageModel ReadStage()
    {
        StageModel stage = new StageModel();
        stage.stage_01 = PlayerPrefs.GetInt("stage_01", 1);
        stage.stage_02 = PlayerPrefs.GetInt("stage_02", 0);
        stage.stage_03 = PlayerPrefs.GetInt("stage_03", 0);
        stage.stage_04 = PlayerPrefs.GetInt("stage_04", 0);
        return stage;
    }

    public void WriteStage(StageModel stage = null)
    {
        PlayerPrefs.SetInt("stage_01", stage.stage_01);
        PlayerPrefs.SetInt("stage_02", stage.stage_02);
        PlayerPrefs.SetInt("stage_03", stage.stage_03);
        PlayerPrefs.SetInt("stage_04", stage.stage_04);

        //LocalStoreHelper.Store(stage, Engine.File("StageData"), LocalStoreHelper.ObjectType.Object);
    }
}
